<template>
    <div class="mod-config">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
                    <el-form-item>
                        <el-input v-model="dataForm.leiXingMingCheng" placeholder="类型名称" clearable></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="dataForm.leiXingMiaoShu" placeholder="类型描述" clearable></el-input>
                    </el-form-item>

            <el-form-item>
                <el-button @click="getDataList(1)">查询</el-button>
                <el-button type="primary" @click="addOrUpdateHandle()">新增
                </el-button>
                <el-button type="danger" @click="deleteHandle()"
                           :disabled="dataListSelections.length <= 0">批量删除
                </el-button>
            </el-form-item>
        </el-form>
        <el-table
                :data="dataList"
                border
                row-key="id"
                v-loading="dataListLoading"

                style="width: 100%;">
<!--            <el-table-column
                    label="选中"
                    type="selection"
                    header-align="center"
                    align="center"
                    width="50">
            </el-table-column>-->
<!--            <el-table-column
                    type="index"
                    label="序号"
                    header-align="center"
                    align="center"
                    width="50">
            </el-table-column>-->
                    <el-table-column
                            prop="leiXingMingCheng"
                            header-align="center"
                            align="center"
                            label="类型名称">
                    </el-table-column>
                    <el-table-column
                            prop="leiXingMiaoShu"
                            header-align="center"
                            align="center"
                            label="类型描述">
                    </el-table-column>
                    <el-table-column
                            prop="tianJiaShiJian"
                            header-align="center"
                            align="center"
                            label="添加时间">
                    </el-table-column>

            <el-table-column
                    fixed="right"
                    header-align="center"
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small"
                               @click="infoHandle(scope.row.id)"><i class="el-icon-edit"></i>查看详情</el-button>
                    <el-button type="text" size="small"
                               @click="addOrUpdateHandle(scope.row.id)"><i class="el-icon-edit"></i>修改</el-button>
                    <el-button type="text" size="small" v-if="scope.row.parentId !='-1'"
                               @click="deleteHandle(scope.row.id)"><i class="el-icon-delete"></i>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <el-dialog title="添加或编辑" :visible.sync="addOrUpdateVisible">
            <el-form :rules="dataRule" :model="dataSaveForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
                <el-form-item hidden :label-width="'120px'"  prop="id">
                    <el-input v-model="dataSaveForm.id" autocomplete="off"></el-input>
                </el-form-item>
                        <el-form-item label="类型名称" :label-width="'120px'"  prop="leiXingMingCheng">
                            <el-input v-model="dataSaveForm.leiXingMingCheng" autocomplete="off"></el-input>
                        </el-form-item>
              <el-form-item label="所属上级" :label-width="'120px'"  prop="parentId">

                <treeselect ref="lxTree" v-model="dataSaveForm.parentId" :options="leiXingList" :props="props" style="width: 100%"
                            @valHandler="getSelected"  placeholder="请选择归属类型" />
                <span style="font-size: 12px;color: red">不选择上级默认一级</span>
              </el-form-item>
                        <el-form-item label="类型描述" :label-width="'120px'"  prop="leiXingMiaoShu">
                            <el-input type="textarea" v-model="dataSaveForm.leiXingMiaoShu" autocomplete="off"></el-input>
                        </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="qx()">取 消</el-button>
                <el-button type="primary" @click="dataFormSubmit()">确 定</el-button>
            </div>
        </el-dialog>


        <!-- 弹窗, 新增 / 修改 -->
        <el-dialog title="药材资讯类型" :visible.sync="infoVisible">
            <div class="mod-config">
                <el-descriptions :column="2"  :colon="false" title="资讯类型详情" content-class-name="customConten" label-class-name="customelable">
                            <el-descriptions-item label="类型名称">{{this.infoList.leiXingMingCheng}}</el-descriptions-item>
                            <el-descriptions-item label="类型描述">{{this.infoList.leiXingMiaoShu}}</el-descriptions-item>
                            <el-descriptions-item label="添加时间">{{this.infoList.tianJiaShiJian}}</el-descriptions-item>
                </el-descriptions>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="qxinfo()">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import treeselect from "../../../components/tree-select";
import { treeDataTranslate } from '../../../utils';
    export default {
      components:{
        treeselect,
      },
        data() {
            return {
                dataForm: {
                                leiXingMingCheng:'',
                                leiXingMiaoShu:'',

                },
                dataSaveForm: {
                                leiXingMingCheng:'',
                                leiXingMiaoShu:'',
                                parentId:''

                },
                dataRule: {
                                leiXingMingCheng: [
                                {required: true, message: '不能为空', trigger: 'blur'}
                            ],
                                leiXingMiaoShu: [
                                {required: true, message: '不能为空', trigger: 'blur'}
                            ],

                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false,

                infoList:[],
                infoLoading: false,
                infoVisible: false,
              props: { // 配置项（必选）
                value: 'id',
                label: 'leiXingMingCheng',
                children: 'children'
              },
              leiXingList:[],
            }
        },

        activated() {
          this.getLeiXingList()
            this.getDataList()
        },
        methods: {
            getDataList(pageIndex) {
                if (pageIndex) {
                    this.pageIndex = pageIndex
                }
                this.dataListLoading = true
                this.$http({
                    url: this.$http.adornUrl('/api-trading/yaoCaiZiXun/yaoCaiZiXunZiXunLeiXing/list'),
                        method: 'get',
            params: this.$http.adornParams({
                'page': this.pageIndex,
                        'limit': this.pageSize,
                'leiXingMingCheng': this.dataForm.leiXingMingCheng,
                'leiXingMiaoShu': this.dataForm.leiXingMiaoShu,

            })
            }).then(({data}) => {
                    if (data && data.code === 0) {
                        // this.dataList = data.data.records
                      this.dataList = treeDataTranslate(data.data.records, 'id','parentId');
                        this.totalPage = Number(data.data.total)
                    } else {
                        this.dataList = []
                        this.totalPage = 0
                    }
                    this.dataListLoading = false
                })
            },
          getLeiXingList(){
            this.leiXingList=[]
            this.$http({
              url: this.$http.adornUrl(`/api-trading/yaoCaiZiXun/yaoCaiZiXunZiXunLeiXing/allList`),
              method: 'get',
              // params: this.$http.adornParams({"id":id})
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.leiXingList = treeDataTranslate(data.data, 'id', 'parentId')
                console.log('wwwwww'+this.leiXingList)

              }
            })
          },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 多选
            selectionChangeHandle(val) {
                this.dataListSelections = val
            },
            qx() {
                this.addOrUpdateVisible = false;

            },
          // 菜单树选中
          getSelected(e){
            // console.log("444444444444")
            this.dataSaveForm.parentId=e.id
          },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true;
              this.getLeiXingList()
              this.dataSaveForm.parentId=''
              // this.$refs.lxTree.clearHandle()
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (id) {
                        this.$http({
                    url: this.$http.adornUrl(`/api-trading/yaoCaiZiXun/yaoCaiZiXunZiXunLeiXing/getById`),
                                method: 'get',
                                params: this.$http.adornParams({"id":id})
                    }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataSaveForm.id = data.data.id
                                this.dataSaveForm.leiXingMingCheng = data.data.leiXingMingCheng;
                                this.dataSaveForm.leiXingMiaoShu = data.data.leiXingMiaoShu;
                              this.dataSaveForm.parentId = data.data.parentId;
                            }
                        })
                    }
                })

            },
            qxinfo() {
                this.infoVisible = false;

            },
            // 新增 / 修改
            infoHandle(id) {
                this.infoVisible = true;
                this.infoList=[];
                this.$http({
            url: this.$http.adornUrl(`/api-trading/yaoCaiZiXun/yaoCaiZiXunZiXunLeiXing/getById`),
                        method: 'get',
                        params: this.$http.adornParams({"id":id})
            }).then(({data}) => {
                    if (data && data.code === 0) {
                        this.infoList=data.data;
                    }
                })
            },
            // 删除
            deleteHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.id
                })
                this.$confirm(`确定要进行删除操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/api-trading/yaoCaiZiXun/yaoCaiZiXunZiXunLeiXing/delByIds'),
                            method: 'post',
                            data: this.$http.adornData(ids, false)
                }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getDataList()
                                }
                            })
                        } else {
                        this.$message.error(data.msg)
                        }
                    })
                })
            },
            // 表单提交
            dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                    url: this.$http.adornUrl(`/api-trading/yaoCaiZiXun/yaoCaiZiXunZiXunLeiXing/save`),
                                method: 'post',
                    data: this.$http.adornData({
                    'id': this.dataSaveForm.id || undefined,
                        'leiXingMingCheng': this.dataSaveForm.leiXingMingCheng,
                        'leiXingMiaoShu': this.dataSaveForm.leiXingMiaoShu,
                      'parentId': this.dataSaveForm.parentId,
                    })
                    }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.addOrUpdateVisible = false
                                        this.getDataList()
                                    }
                                })
                            } else {
                            this.$message.error(data.msg)
                            }
                        })
                    }
                })


            },
        }
    }
</script>
<style>
.customConten{
  color: rgb(19 35 29)
}
.customelable{
  color: rgb(19 35 29 / 77%);text-align:right;width:120px;background-color:#10592a0d;font-weight:bold
}
</style>
